package com.xing.Common.Dao.Imp;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.xing.Common.Dao.CommonDao;

/**
 * @author xingxing
 * Dao层实现
 */
@Repository("commonDao")
public class CommonDaoImp implements CommonDao{
	@Autowired
	HibernateTemplate hibernateTemplate;
	

	@Override
	public boolean saveorupd(Object obj) {
		try { 
			hibernateTemplate.saveOrUpdate(obj);
			return true;
		}catch (Exception e) {
			return false;
		}
	}

	@Override
	public boolean del(Object obj) {
		try { 
			hibernateTemplate.delete(obj);
			return true;
		}catch (Exception e) {
			return false;
		}
	}



	@Override
	public List<?> objectfindall(Class clazz) {
		hibernateTemplate.find("from "+clazz.getName());
		return null;
	}

	@Override
	public List<?> objectfind(Object obj) {	
		return hibernateTemplate.findByExample(obj);
	}
	
	@Override
	public List createSql(String sql) {
		// TODO Auto-generated method stub
		Session session=hibernateTemplate.getSessionFactory().openSession();
		Transaction ts= session.beginTransaction();
		List list=null;
		try {
			list=session.createSQLQuery(sql).list();
			ts.commit();
		}catch (Exception e) {
			try {
				ts.rollback();
			} catch (Exception e1) {
				e1.printStackTrace();
			} 
		}finally {
			session.close();
		}
		
		return list;
	}

	
	public CommonDaoImp(){
		System.out.println("----CommonDaoImp----");
	}
	
}
